using System.Collections.Generic;
using ComponentSystem;
using UnityEngine;
using ZYFramework.ObjectPool;

namespace ZYFramework.AudioSystem
{
    /// <summary>
    /// 3D音效位置同步组件
    /// </summary>
    public class Com_Audio3DUpdater : ComponentWithId
    {
        /// <summary>
        /// 3D音效绑定信息
        /// </summary>
        private struct Audio3DBinding
        {
            public GameObject AudioSourceObj;
            public Transform BindTransform;
        }

        /// <summary>
        /// 3D音效绑定列表
        /// </summary>
        private readonly List<Audio3DBinding> _3DBindings = new List<Audio3DBinding>();

        /// <summary>
        /// 父实体（用于访问LateUpdate）
        /// </summary>
        private ComponentSystem.EntityMono _parentMono;

        protected override void Awake()
        {
            base.Awake();
        }

        protected override void Start()
        {
            base.Start();
        }

        /// <summary>
        /// 初始化3D更新器
        /// </summary>
        /// <param name="parentMono">父Mono实体</param>
        public void Initialize(ComponentSystem.EntityMono parentMono)
        {
            this._parentMono = parentMono;
        }

        /// <summary>
        /// 添加3D音效绑定
        /// </summary>
        /// <param name="audioSourceObj">AudioSource GameObject</param>
        /// <param name="bindTransform">绑定的Transform</param>
        /// <param name="priority">优先级</param>
        public void Add3DBinding(GameObject audioSourceObj, Transform bindTransform)
        {
            if (audioSourceObj == null || bindTransform == null)
            {
                return;
            }

            // 检查是否已存在
            for (int i = _3DBindings.Count - 1; i >= 0; i--)
            {
                if (_3DBindings[i].AudioSourceObj == audioSourceObj)
                {
                    // 更新绑定信息
                    var binding = _3DBindings[i];
                    binding.BindTransform = bindTransform;
                    _3DBindings[i] = binding;
                    return;
                }
            }

            // 添加新绑定
            _3DBindings.Add(new Audio3DBinding
            {
                AudioSourceObj = audioSourceObj,
                BindTransform = bindTransform
            });
        }

        /// <summary>
        /// 移除3D音效绑定
        /// </summary>
        /// <param name="audioSourceObj">AudioSource GameObject</param>
        public void Remove3DBinding(GameObject audioSourceObj)
        {
            if (audioSourceObj == null)
            {
                return;
            }

            for (int i = _3DBindings.Count - 1; i >= 0; i--)
            {
                if (_3DBindings[i].AudioSourceObj == audioSourceObj)
                {
                    _3DBindings.RemoveAt(i);
                    break;
                }
            }
        }

        /// <summary>
        /// 更新所有3D音效位置（在LateUpdate中调用）
        /// </summary>
        public void Update3DPositions()
        {
            for (int i = _3DBindings.Count - 1; i >= 0; i--)
            {
                var binding = _3DBindings[i];

                // 检查AudioSource是否已被销毁
                if (binding.AudioSourceObj == null)
                {
                    _3DBindings.RemoveAt(i);
                    continue;
                }

                // 检查绑定的Transform是否已被销毁
                if (binding.BindTransform == null)
                {
                    _3DBindings.RemoveAt(i);
                    continue;
                }

                // 更新AudioSource位置
                binding.AudioSourceObj.transform.position = binding.BindTransform.position;
            }
        }

        /// <summary>
        /// 清除所有3D音效绑定
        /// </summary>
        public void ClearAllBindings()
        {
            _3DBindings.Clear();
        }

        /// <summary>
        /// 获取当前绑定的3D音效数量
        /// </summary>
        /// <returns>数量</returns>
        public int GetBindingCount()
        {
            return _3DBindings.Count;
        }
    }
}
